package com.zyd.shiro.controller.admin;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.zyd.shiro.business.vo.OrderVO;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zyd.shiro.persistence.beans.Order;
import com.zyd.shiro.business.service.MallOrderService;

import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;
/**
 * 控制器层
 * @author RookieWZW
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/admin/order")
public class OrderController {

	@Autowired
	private MallOrderService orderService;
	
	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(value="/all",method=RequestMethod.GET)
	public ResponseVO findAll(){
		 return ResultUtil.success("查询成功",orderService.findAll());
		
	}

	/**
	 * 查询未发货的订单
	 * @return
	 */
	@RequestMapping(value = "/delivery", method =RequestMethod.GET )
	public ResponseVO findAllByDelivery(){
		return ResultUtil.success("查询成功",orderService.findAllByDelivery(10));

	}

	/**
	 * 查询待收货的订单
	 * @return
	 */
	@RequestMapping(value = "/receipt", method =RequestMethod.GET )
	public ResponseVO findAllByReceipt(){
		return ResultUtil.success("查询成功",orderService.findAllByReceipt(10));

	}

	/**
	 * 查询待付款的订单
	 * @return
	 */
	@RequestMapping(value = "/pay", method =RequestMethod.GET )
	public ResponseVO findAllByPay(){
		return ResultUtil.success("查询成功",orderService.findAllByPay(10));

	}

	/**
	 * 查询已完成的订单
	 * @return
	 */
	@RequestMapping(value = "/finish",method = RequestMethod.GET)
	public ResponseVO  findAllByFinish(){
		return ResultUtil.success("查询成功",orderService.getAllByPay_statusAndDelivery_statusAndReceipt_status(20,20,20));
	}

	/**
	 * 查询已取消的订单
	 * @return
	 */
	@RequestMapping(value = "/cancel",method = RequestMethod.GET)
	public ResponseVO findAllByCancel(){
		return ResultUtil.success("查询成功",orderService.findAllByStatus(10));

	}

	/**
	 * 根据ID查询订单详情
	 * @param id ID
	 * @return
	 */
	@RequestMapping(value="findOrderByid/{id}",method=RequestMethod.GET)
	public ResponseVO findOne(@PathVariable Integer id){
		return ResultUtil.success("查询成功", orderService.findOne(id));
		
	}

	/**
	 * 多条件查询
	 * @param searchMap 查询条件封装
	 * @return 分页结果
	 */
	@RequestMapping(value="/findSearch",method=RequestMethod.POST)
	public ResponseVO findSearch(@RequestBody Map searchMap ){
		return ResultUtil.success("查询成功",  orderService.findSearch(searchMap));
	}

	/**
	 * 分页查询全部数据
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/{page}/{size}",method=RequestMethod.GET)
	public ResponseVO findPage(@PathVariable int page,@PathVariable int size){

		Page<Order> pageList = orderService.findPage(page, size);
		return ResultUtil.success("查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) );
	}

	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@RequestMapping(value="/{page}/{size}/{searchMap}",method=RequestMethod.POST)
	public ResponseVO findSearch(@PathVariable String searchMap ,@PathVariable int page,@PathVariable int size){

		Page<Order> pageList = orderService.findSearch(searchMap, page, size);
		return ResultUtil.success("查询成功",  new PageResult(pageList.getTotalElements(), pageList.getContent()) );
	}
	

	/**
	 * 修改价格
	 * @param
	 */
	@RequestMapping(value="/price/{id}",method=RequestMethod.POST)
	public ResponseVO update(@RequestBody Map updateMap ,@PathVariable Integer id ){
		orderService.updatePrice(updateMap,id);
		return ResultUtil.success("修改成功");
	}

	/**
	 * 发货
	 * @param
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/Delivery/{eco}/{eno}/{id}",method = RequestMethod.GET)
	public ResponseVO  Delivery(@PathVariable String eco,@PathVariable String eno,@PathVariable Integer id){
		orderService.UpdateDelivery(eco,eno,id);
		return  ResultUtil.success("发货成功");
	}




}
